package com.atguigu.spzx.manner.service.impl;

import com.atguigu.spzx.manner.mapper.SysMenuMapper;
import com.atguigu.spzx.manner.service.SysMenuService;
import com.atguigu.spzx.manner.util.MenuHelper;
import com.atguigu.spzx.model.entity.AuthContextUtil;
import com.atguigu.spzx.model.entity.SysMenu;
import com.atguigu.spzx.model.entity.SysUser;
import com.atguigu.spzx.model.vo.common.SysMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenuList = sysMenuMapper.selectfindNodes();
       return MenuHelper.buildTree(sysMenuList);

    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.insertMenu(sysMenu);
    }

    @Override
    public void updateSysMenu(SysMenu sysMenu) {
        sysMenuMapper.updateMenu(sysMenu);
    }

    @Override
    public void delete(Long id) {
        sysMenuMapper.deleteSysmenuById(id);
    }

    @Override
    public List<SysMenuVo> findUserMenus() {
        SysUser sysUser = AuthContextUtil.get();
        Long id = sysUser.getId();
        //根据id获角色菜单
        List<SysMenu>list = sysMenuMapper.selectSysMenuList(id);
        List<SysMenu> sysMenuList = MenuHelper.buildTree(list);
        List<SysMenuVo> sysMenuVoList = MenuHelper.buildMenuVoList(sysMenuList);//转换对象

        //获取到的
        return sysMenuVoList;
    }


}
